package com.vectorx.myDesignDemos.src.main.java.单例_静态内部类;

public class Demo {

    public static void main(String[] args) {
        DemoObj instance1 = DemoObj.getInstance();
        DemoObj instance2 = DemoObj.getInstance();
        System.out.println(instance1==instance2);
        System.out.println(instance1.hashCode());
        System.out.println(instance2.hashCode());
    }
}


class DemoObj {

    //构造方法私有化
    private DemoObj() {
    }


    //创建静态内部类，返回实例对象
    private static class innerClass {
        private static DemoObj demoObj = new DemoObj();
    }

    //提供静态公共方法获取实例
    public static DemoObj getInstance() {
        return innerClass.demoObj;
    }
}
